package com.peanutplan.blog.controller;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.peanutplan.blog.dao.EsHotWordMapper;
import com.peanutplan.blog.dao.EsStopWordMapper;
import com.peanutplan.blog.model.vo.EsHotWord;
import com.peanutplan.blog.model.vo.EsStopWord;

import tk.mybatis.mapper.entity.Example;

/**
 * 需修改ik分词器的配置文件<br/>
 * {conf}/analysis-ik/config/IKAnalyzer.cfg.xml<br/>
 * 用户可以在这里配置远程扩展字典 
 * &lt;entry key="remote_ext_dict"&gt;url&lt;/entry&gt;
 * <p>Title: IkRemoteHotWordController</p>
 * <p>Description: </p>
 * @author chenyang-054
 * @date 2020年3月12日
 */
@Controller
public class IkRemoteHotWordController {
	
	@Resource
    private EsHotWordMapper esHotWordMapper;
	
	@Resource
    private EsStopWordMapper esStopWordMapper;
	
	/**
	  * Description: 接收head请求（Last-Modified或ETag发生变化时，请求同名的get方法获取热词），通与判断热词是否变化是否
	  * @param response
	 */
	@RequestMapping(value = "/hotDict", method = RequestMethod.HEAD)
	public void headDict(HttpServletResponse response) {
		String modified = "0";
		Example example = new Example(EsHotWord.class);
		example.setOrderByClause("created desc");
		PageHelper.startPage(1, 1);
		List<EsHotWord> list = esHotWordMapper.selectByExample(example);
		if(CollectionUtils.isNotEmpty(list)) {
			modified = list.get(0).getCreated().toString();
		}
		
		response.setHeader("Last-Modified", modified);
		response.setHeader("ETag",modified);
		response.setContentType("text/plain; charset=UTF-8");
	}
	
	/**
	 * head发生变化，获取热词
	 *<p>Title: loadheadDict</p>
	 *<p>Description: </p>
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value = "/hotDict", method = RequestMethod.GET, produces = "text/plain; charset=utf-8")
	public String loadheadDict(HttpServletResponse response) {
		StringBuilder hotwordStr=new StringBuilder();
		List<String> hotWord = esHotWordMapper.selectAll().stream().map(EsHotWord::getHotWord).collect(Collectors.toList());
		hotWord.forEach(str->{
	        hotwordStr.append(str+"\r\n");
	    });
		return hotwordStr.toString();
	}
	
	@RequestMapping(value = "/stopDict", method = RequestMethod.HEAD)
	public void stopDict(HttpServletResponse response) {
		String modified = "0";
		Example example = new Example(EsStopWord.class);
		example.setOrderByClause("created desc");
		PageHelper.startPage(1, 1);
		List<EsStopWord> list = esStopWordMapper.selectByExample(example);
		if(CollectionUtils.isNotEmpty(list)) {
			modified = list.get(0).getCreated().toString();
		}
		
		response.setHeader("Last-Modified", modified);
		response.setHeader("ETag",modified);
		response.setContentType("text/plain; charset=UTF-8");
	}
	
	/**
	 * head发生变化，获取热词
	 *<p>Title: loadheadDict</p>
	 *<p>Description: </p>
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value = "/stopDict", method = RequestMethod.GET, produces = "text/plain; charset=utf-8")
	public String loadStopDict(HttpServletResponse response) {
		StringBuilder stopwordStr=new StringBuilder();
		List<String> stopWord = esStopWordMapper.selectAll().stream().map(EsStopWord::getStopWord).collect(Collectors.toList());
		stopWord.forEach(str->{
	        stopwordStr.append(str+"\r\n");
	    });
		return stopwordStr.toString();
	}
}
